package org.shj.basic

object Basic {
  def main(args: Array[String]): Unit = {
    val age = 20
    val result = if (age < 18 ) "young" else "adult"
    println(result)
    
    for(year <- 2018 to 2018){
      println("11111111") //会执行一次
    }
    
    println("============= 测试  par")
    // par 方法产出当前集合的一个并行实现, 下面输出可看出，并不是按顺序输出的
    for(i <- (0 until 100).par) print(i + " ")
    
    println
    println("============= 测试  par")
    
    for(i <- 0 to 5 if i % 2 == 0)
      println(i)
      
    import scala.util.control.Breaks._
    
    breakable{
    for(item <- "Spark")
      if(item == 'r')
        break        // break与breakable是配对使用的
      else
        print(item + " ")
    }    
    println()
    
    println("fibonacci(30) = " + fibonacci(10))    
    println(sum(1 to 10 : _*))
    
    println("""Welcome to the Scala.
          You are right.""") //会多出来很多空格。利用下面的方法可以去掉第二行的空格
    println("""|Welcome to the Scala.
          |You are right.""".stripMargin) 
          
    val a = new String("abc")
    val b = new String("abc")
    println("a==b is " + (a==b))
    println("a eq b is " + (a eq b))
  }
  
  def sum(num : Int*) : Int = {
    var result = 0
    for(n <- num)
      result += n
    result
  }
  
  def fibonacci(n : Long) : Long = {
    if(n <= 2)
      1
    else
      fibonacci(n-2) + fibonacci(n-1)
  }
}